package com.cgly.medical.controller;

import com.cgly.medical.constants.MqConstants;
import com.cgly.medical.domain.Patient;
import com.cgly.medical.service.PatientService;
import com.cgly.medical.util.Msg;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin  //解决跨域问题
public class PatientController {

    @Resource
    PatientService patientService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("patient/getall/{page}/{limit}")
    public Msg getAllPatient(@PathVariable Integer page, @PathVariable Integer limit){
        Msg msg = patientService.getAllPatient(page,limit);
        return msg;
    }

    @PostMapping("patient/getsome/{page}/{limit}")
    public Msg getSomePatient(@PathVariable Integer page, @PathVariable Integer limit,@RequestBody(required = false)Patient patient){
        patient.setPn(page);
        patient.setSize(limit);
        Msg msg=null;
//        System.out.println("======="+patient.getPname());
//        if("".equals(patient.getPname())||patient.getPname()==null){
//            msg=patientService.getSomePatient(patient);
//        }else{
//            msg = patientService.highlightShow(patient);
//        }
        msg = patientService.highlightShow(patient);
        return msg;
    }



    @DeleteMapping("patient/delete/{id}")
    public Msg deletePatientById(@PathVariable Integer id){
        Msg msg = patientService.deletePatientById(id);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_DELETE_KEY, id);
        return msg;
    }

    @PostMapping("patient/update")
    public Msg updatePatient(@RequestBody(required = false) Patient patient){

        Msg msg = patientService.updatePatient(patient);
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, patient.getId());
        return msg;
    }

    @PostMapping("patient/insert")
    public Msg insertPatient(@RequestBody(required = false) Patient patient){
        Msg msg = patientService.insertPatient(patient);
//        int maxId = patientService.getMaxId();
//        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, maxId);
        return msg;
    }

    @GetMapping("patient/getone/{id}")
    public Msg getPatientById(@PathVariable Integer id){
        Msg msg = patientService.getPatientById(id);
        return msg;
    }

    @PostMapping("patient/suggest")
    public Msg suggestPatient(@RequestBody(required = false) Patient patient){
        System.out.println(patient.getPname());
//        Msg msg = patientService.getSuggestions(patient.getPname());
        return null;
    }

    @PostMapping("patient/addORupdate")
    public Msg addOrUpdatePatient(@RequestBody(required = false) Patient patient){
        Msg msg = patientService.addOrUpdatePatient(patient);
        return msg;
    }

}
